import { Component, OnInit, ViewChild } from '@angular/core'
import { Router, ActivatedRoute } from '@angular/router' //路由引入
import { JsonApiService } from 'app/core/api/json-api.service'
import { config } from '../../shared/smartadmin.config'
import { DatePickerModule } from '../../shared/date-picker/date-picker.module'
import { throttleable } from '@swimlane/ngx-datatable/release/utils'

declare var $: any
@Component({
  selector: 'sa-renewal-record',
  templateUrl: './renewal-record.component.html',
  styleUrls: ['./renewal-record.component.css']
})
export class RenewalRecordComponent implements OnInit {
  tableData: any = [] // table的表单数据
  totalCounts: number = 0 //分页总条数
  curPage: number = 1 //分页当前页
  pageSize: number = 10 //内置的分页每页条数
  activeSelected: string = '' //row高亮
  // 查询参数
  selectedCompanyId: any = '' // 选中的公司id
  selectedCompanyName: any = '' // 选中的公司名称
  selectedDeviceId: any = '' // 选中的设备ID
  selectedCarNumber: any = '' // 选中的车牌号
  searchKey: any = '' // 用户的搜索关键字
  sortTime: any = '2' // 升降序的标志
  dateType: any = '1'
  dataSourceType: any = ''
  requestType: any = ''
  // 标志是否选择车辆的
  isShowDelete: any = false //是否有删除权限
  specifyTime: {
    startTime: ''
    endTime: ''
  }
  startTime_search: string = ''
  endTime_search: string = ''
  billType: number = 1
  specifyId = {
    picker1: 'datetimepicker3',
    picker2: 'datetimepicker4'
  }

  constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
    activatedRoute.queryParams.subscribe(queryParams => {})
  }
  ngOnInit() {
    // this.load()
  }

  load() {
    let param = {
      pageSize: this.pageSize,
      nowPage: this.curPage,
      parameters: {
        companyId: this.selectedCompanyId,
        deviceId: this.selectedDeviceId,
        // carNumber: this.selectedCarNumber,
        searchKey: common.stringTrim(this.searchKey),
        startTime: this.startTime_search,
        endTime: this.endTime_search,
        // sortTime: this.sortTime,
        dateType: this.dateType,
        billType: this.billType, //账单类型（1.服务费，2.对接费）
        requestType: this.requestType //请求来源类型 1:联调  2:正式
        // dataSourceType: this.dataSourceType
      }
    }
    let reqdata = 'gridPager=' + JSON.stringify(param)
    this.jsonApiService.http('renewalTestTemp/pageList.html', reqdata).subscribe(res => {
      const result = res.data
      let data = []
      if (result.isSuccess && result.exhibitDatas) {
        data = result.exhibitDatas
        this.tableData = result.exhibitDatas
        this.totalCounts = result.recordCount
      }
    })
  }
  changeDateType() {
    this.load()
  }
  // 接收子组件传递过来的数据
  getSelectTableTime(time) {
    this.startTime_search = time.startTime
    this.endTime_search = time.endTime
    this.tableSearch()
  }
  //公司选中回调
  companySelected(data) {
    this.selectedDeviceId = data.deviceId
    this.selectedCarNumber = data.carNumber
    this.selectedCompanyId = data.companyId
    this.selectedCompanyName = data.companyName
  }
  // 分页回调
  paginate(e) {
    this.activeSelected = ''
    this.curPage = e.page + 1
    this.pageSize = e.rows
    this.load()
  }
  // 刷新表
  refresh() {
    this.load()
  }
  // 切换倒序升序
  columnSort(sort, type) {
    this.sortTime = sort.toString()
    this.dateType = type.toString()
    this.load()
  }
  // 删除续费记录
  del_domCode(row) {
    this.jsonApiService.confirm_message('此操作将永久删除数据,确认要删除').then(() => {
      this.jsonApiService.http_get('renewal/v2/remove.html?id=' + row.id).subscribe(data => {
        this.jsonApiService.message(data.message)
        if (data.success) {
          this.load()
        }
      })
    })
  }
  // 表单查询
  tableSearch() {
    this.curPage = 1
    this.load()
  }

  tabChange(type) {
    this.billType = type
    this.curPage = 1
    this.load()
  }
}
